package com.traffic.api.filter.filter;

import java.util.Map;

import org.apache.log4j.Logger;

import com.traffic.api.exception.ApiBizException;
import com.traffic.api.jedis.JedisClient;

public class IPbindingFilter implements IApiFilter {
	private static Logger log = Logger.getLogger(IPbindingFilter.class);

	public static String cacheStart_bindips = "bindips_";

	public void validateFilter(Map<String, Object> params) throws ApiBizException {
		JedisClient client = JedisClient.getInstance();
		boolean isExists = client.exists(cacheStart_bindips + params.get("appkey"));
		if (isExists) {
			String ips = (String) client.get(cacheStart_bindips + params.get("appkey"));
			ips = ips.replaceAll(",", "");
			String ip = (String) params.get("ip");
			if ((null != ips) && (!("".equals(ips))) && (!(ips.contains(ip)))) {
				log.debug("ip [" + ip + "] is blocked,appkey:" + params.get("appkey"));
				throw new ApiBizException(new Throwable("(api)您的ip未绑定: " + ip));
			}
			params.remove("ip");
		}
	}

	public static void main(String[] args) {
	}
}